﻿using DataSync.Common;
using DataSync.Entity;
using Boco.CommonLib;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataSync.Proc
{
    class DBSqlExe : TaskBase
    {
        private static ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        DBConfig CurDbConf;

        public DBSqlExe(TaskConf conf) : base(conf)
        {
        }

        public override void Init()
        {
            var config = TaskConfig.GetTaskConfig(CurTaskConfig.Id);

            //读取配置
            var dbconf = config.Element("DBConfig");
            if (dbconf == null)
            {
                throw new ApplicationException(string.Format("任务{0}-DBConfig配置不正确", CurTaskConfig.Name));
            }
            CurDbConf = new DBConfig()
            {
                //DBType = dbconf.GetAttribute("DBType"),
                DBType = (DataBaseType)Enum.Parse(typeof(DataBaseType), dbconf.GetAttribute("DBType"), false),
                ConnString = dbconf.GetAttribute("ConnString")
            };
            CurDbConf.Sqls = new Dictionary<string, string>();
            var sqls = dbconf.Element("Sqls").Elements("sql");
            foreach (var sqlitem in sqls)
            {
                var id = sqlitem.GetAttribute("id");
                if (CurDbConf.Sqls.ContainsKey(id))
                {
                    CurDbConf.Sqls[id] = sqlitem.Value;
                }
                else
                {
                    CurDbConf.Sqls.Add(id, sqlitem.Value);
                }
            }

            //检查正确性
            //if (string.IsNullOrEmpty(CurDbConf.DBType))
            //{
            //    throw new ApplicationException(string.Format("任务{0}-DBConfig:DBType配置不正确", CurTaskConfig.Name));
            //}
            if (string.IsNullOrEmpty(CurDbConf.ConnString))
            {
                throw new ApplicationException(string.Format("任务{0}-DBConfig:ConnString配置不正确", CurTaskConfig.Name));
            }
            if (CurDbConf.Sqls.Count == 0)
            {
                throw new ApplicationException(string.Format("任务{0}-DBConfig:Sql配置不正确,没有需要执行的sql", CurTaskConfig.Name));
            }
        }

        public override void Run()
        {
            using (var conn = DBFactory.GetConn(this.CurDbConf))
            {
                conn.Open();
                using (var trs = conn.BeginTransaction())  //开启事务
                {
                    foreach (var item in this.CurDbConf.Sqls)
                    {
                        if (string.IsNullOrEmpty(item.Value))
                        {
                            continue;
                        }
                        log.InfoFormat("执行sql：{0}-> {1}", item.Key, item.Value);
                        var cmd = conn.CreateCommand();
                        cmd.CommandText = item.Value;
                        var num = cmd.ExecuteNonQuery();
                        log.InfoFormat("执行sql：{0}完成，本次影响数据:{1}条", item.Key, num);
                    }
                    trs.Commit();
                }
            }
        }
    }
}
